<?php
	$cache_time=1;
	$OJ_CACHE_SHARE=false;
	require_once("config/path_config.php");
	require_once("config/sys_config.php");
    require_once('./include/cache_start.php');
    require_once('./include/db_info.inc.php');
    require_once('./include/const.inc.php');
	require_once('./include/setlang.php');
	$view_title=$MSG_SUBMIT;

	// 检查用户是否已登录
	if (!isset($_SESSION['user_id'])){
		$view_errors= "<a href=loginpage.php>$MSG_Login</a>";
		require("template/".$OJ_TEMPLATE."/error.php");
		exit(0);
	}

	if ( isset($_SESSION['parent']) && $_SESSION['parent'] ) {
    	$view_errors= "为维护正常的教学秩序和方便老师跟踪学员学习情况，家长的账号权限如下：<br/>";
        $view_errors.= "1. 允许查看所有代码，跟踪学员学习情况<br/>";
        $view_errors.= "2. 允许查看标准代码，便于部分家长和学员自学<br/>";
        $view_errors.= "3. 不允许提交代码，便于老师管理和跟踪学员学习和作业完成情况<br/>";
        $view_errors.= "4. 为杜绝学员自己不思考直接抄答案，家长的账号仅限家长自己使用<br/>";
        require("template/".$OJ_TEMPLATE."/error.php");
    	exit(0);
	}

	// 查找指定ID的题目信息
	if (isset($_GET['id'])){
		$id=intval($_GET['id']);
        $sample_sql="select sample_input,sample_output,problem_id from problem where problem_id=?";
	}else if (isset($_GET['cid'])&&isset($_GET['pid'])){
		$cid=intval($_GET['cid']);$pid=intval($_GET['pid']);
        $sample_sql="select sample_input,sample_output,problem_id from problem where problem_id in
		(select problem_id from contest_problem where contest_id=? and num=?)";
	}else{
		$view_errors=  "<h2>No Such Problem!</h2>";
		require("template/".$OJ_TEMPLATE."/error.php");
		exit(0);
	}

	$view_src="";
	$ok=false;
	if(isset($_GET['sid'])){
		$sid=intval($_GET['sid']);
		$sql="SELECT * FROM `solution` WHERE `solution_id`=?";
		$result=pdo_query($sql,$sid);
		$row=$result[0];

        $sessionUserId = "";
        $sourceUserId = "";
        if ( isset($_SESSION['user_id']) ) {
            $sessionUserId = strtolower($_SESSION['user_id']);
        }
        if ( $row ) {
            $sourceUserId = strtolower($row['user_id']);
        }
		if ( $row && $sessionUserId == $sourceUserId) {
            $ok = true;
        }
	}
	else{
		if(isset($OJ_EXAM_CONTEST_ID)){
			if($cid<$OJ_EXAM_CONTEST_ID&&!isset($_SESSION['source_browser'])){
				header("Content-type: text/html; charset=utf-8");
				echo $MSG_SOURCE_NOT_ALLOWED_FOR_EXAM;
				exit();
			}
		}
	}
	
	if ($ok){
		$sql="SELECT `source` FROM `source_code_user` WHERE `solution_id`=?";
		$result=pdo_query($sql,$sid);
		$row=$result[0];
		if($row) {
			$view_src=$row['source'];
		}
	}

	if(isset($id))$problem_id=$id;
	$view_sample_input="1 2";
	$view_sample_output="3";

	if(isset($sample_sql)){
		if (isset($_GET['id'])){
			$result=pdo_query($sample_sql,$id);
		}else{
			$result=pdo_query($sample_sql,$cid,$pid);
		}
		$row=$result[0];
		$view_sample_input=$row[0];
		$view_sample_output=$row[1];
		$problem_id=$row[2];
	}

	if(!$view_src){
	if(isset($_COOKIE['lastlang'])) 
		$lastlang=intval($_COOKIE['lastlang']);
	else 
		$lastlang=0;
	$template_file="$OJ_DATA/$problem_id/template.".$language_ext[$lastlang];
   if(file_exists($template_file)){
	$view_src=file_get_contents($template_file);
   }

}
	$sql="SELECT count(1) FROM `solution` WHERE result<4";
	$result=pdo_query($sql);
	 $row=$result[0];
	if($row[0]>10) {
		$OJ_VCODE=true;
	}
	


/////////////////////////Template
require("template/bs3/submitpage.php");
/////////////////////////Common foot
if(file_exists('./include/cache_end.php'))
	require_once('./include/cache_end.php');
?>

